import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;


public class Solution {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n= Integer.parseInt(sc.nextLine());
		int k = Integer.parseInt(sc.nextLine());
		int i = 0;
		List<Integer> choclatePackets = new ArrayList<Integer>();
		
		while(i++ < n){
			String line = sc.nextLine();
			choclatePackets.add(Integer.parseInt(line.trim()));
		}
		
		Solution code = new Solution();
		code.sort(choclatePackets);		
		System.out.println(code.minimumUnfairness(choclatePackets,k));
		sc.close();
	}
	public void sort(List<Integer> choclatePackets){
		Collections.sort(choclatePackets);		
	}
	public int minimumUnfairness(List<Integer> choclatePackets,int k){
		List<Integer> unfairnessValues = new ArrayList<Integer>();
		for (int i = 0; i < (choclatePackets.size()-k); i++){
			int unfairness = unfairness(choclatePackets,i,i+k);
			if(unfairness >0 ){
				unfairnessValues.add(unfairness);
			}
		}
		return Collections.min(unfairnessValues);
	}
	public int unfairness(List<Integer> choclatePackets , int from, int to){
		int unfairness = 0;
		if(to < choclatePackets.size()){
			List<Integer> kListChocs = choclatePackets.subList(from, to);
			unfairness = Collections.max(kListChocs) - Collections.min(kListChocs);
		}
		
		return unfairness;
	}

}
